import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
# 第一次启动（开始爬取）
# 再次运行，自动从断点继续
scrapy crawl ydar -s JOBDIR=/home/ccg/crawls/mytask -o ydar.json

scrapy crawl ydar -o ydar.json
https://wbka--se8feirae.lewhhhzzz7y6m1.xyz/xsp
DOWNLOAD_DELAY = 0.3
'''


class YdarSpider(scrapy.Spider):
    name = "ydar"
    # 前缀
    prefix = 'https://wbka--se8feirae.'
    # 中缀
    website = 'lewhhhzzz7y6m1'
    # 后缀
    suffix = '.xyz/'
    allowed_domains = [website + '.xyz']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        tags = response.xpath("//div[@class='breadcrumbs']//a/text()").extract()
        name = response.xpath("//div[@class='breadcrumbs']//span/text()").extract()
        p_url = response.xpath("//div[@class='detail-poster']//a//img/@ src").extract()
        url = response.xpath("//ul[@class='detail-play-list clearfix tab-pane ff-playurl ff-playurl-tab-1 active fade in']//li//a/@ href").extract()
        if len(url) and len(p_url) and len(name) and len(tags):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[-1], url=split_joint(self.prefix + self.website + self.suffix, url[0]), name=name[0], pUrl=p_url[0], vUrl='')
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=format_url_one(video_url[0]))
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                fin_url = split_joint(self.prefix + self.website + self.suffix, url)
                if (not url.startswith(split_joint(self.prefix + self.website + self.suffix, '/arttype/')) or not url.startswith(split_joint(self.prefix + self.website + self.suffix, '/art/cover/id/')) or not url.startswith(split_joint(self.prefix + self.website + self.suffix, '/artdetail-'))):
                    yield scrapy.Request(fin_url, callback=self.parse)